package com.huibur.devicecheck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huibur.devicecheck.exception.CheckItemNameIsRepeatException;
import com.huibur.devicecheck.mapper.CheckItemMapper;
import com.huibur.devicecheck.model.param.CheckItemParam;
import com.huibur.devicecheck.model.po.CheckItemPO;
import com.huibur.devicecheck.model.po.TaskPO;
import com.huibur.devicecheck.model.query.CheckItemQuery;
import com.huibur.devicecheck.model.vo.CheckItemVO;
import com.huibur.devicecheck.model.vo.TaskVO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.request.SessionContext;
import com.huibur.devicecheck.service.ICheckItemService;
import com.huibur.devicecheck.utils.ConvertPageUtil;
import com.huibur.devicecheck.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*******************************************************
* @title: CheckItemServiceImpl.java
* @description:
* @author wushiqi
* @date 2021/9/30
* @version 1.0
*******************************************************/
@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItemPO> implements ICheckItemService {

    @Autowired
    CheckItemMapper checkItemMapper;

    /**
     * 新增检查项
     * @param checkItemParam
     * @return
     */
    @Override
    public ObjRes<Void> insert(CheckItemParam checkItemParam) {
        CheckItemPO checkItem = checkItemMapper.selectOne(new LambdaQueryWrapper<CheckItemPO>()
                .eq(CheckItemPO::getDeviceName, checkItemParam.getDeviceName())
                .eq(CheckItemPO::getName, checkItemParam.getName())
                .eq(CheckItemPO::getIsDeleted, 0));
        if (null != checkItem) {
            throw new CheckItemNameIsRepeatException();
        }

        CheckItemPO checkItemPO = new CheckItemPO();
        checkItemPO.setDeviceName(checkItemParam.getDeviceName());
        checkItemPO.setInputeMode(checkItemParam.getInputeMode());
        checkItemPO.setText(checkItemParam.getText());
        checkItemPO.setName(checkItemParam.getName());
        checkItemPO.setIsDeleted(0);
        checkItemPO.setCreateAt(DateUtils.getCurrentTimestamp());
        checkItemPO.setCreateBy(SessionContext.getCurrentUserSubject().getUserId());
        checkItemMapper.insert(checkItemPO);
        return new ObjRes<Void>().success("新增成功");
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Long id) {
        CheckItemPO checkItemPO = new CheckItemPO();
        checkItemPO.setId(id);
        checkItemPO.setIsDeleted(1);
        checkItemMapper.updateById(checkItemPO);
        return true;
    }

    /**
     * 编辑检查项
     * @param id
     * @param checkItemParam
     * @return
     */
    @Override
    public ObjRes<Void> updateCheckItem(Long id, CheckItemParam checkItemParam) {
        CheckItemPO checkItem = checkItemMapper.selectOne(new LambdaQueryWrapper<CheckItemPO>()
                .ne(CheckItemPO::getId,id)
                .eq(CheckItemPO::getDeviceName, checkItemParam.getDeviceName())
                .eq(CheckItemPO::getName, checkItemParam.getName())
                .eq(CheckItemPO::getIsDeleted, 0));
        if (null != checkItem) {
            throw new CheckItemNameIsRepeatException();
        }

        CheckItemPO checkItemPO = new CheckItemPO();
        checkItemPO.setDeviceName(checkItemParam.getDeviceName());
        checkItemPO.setInputeMode(checkItemParam.getInputeMode());
        checkItemPO.setText(checkItemParam.getText());
        checkItemPO.setName(checkItemParam.getName());
        checkItemPO.setIsDeleted(0);
        checkItemPO.setUpdateAt(DateUtils.getCurrentTimestamp());
        checkItemPO.setUpdateBy(SessionContext.getCurrentUserSubject().getUserId());
        checkItemMapper.update(checkItemPO,new LambdaQueryWrapper<CheckItemPO>()
                .eq(CheckItemPO::getId,id));
        return new ObjRes<Void>().success("修改成功");
    }

    /**
     * 检查项列表(查询并分页)
     * @param checkItemQuery
     * @return
     */
    @Override
    public PageRes<CheckItemVO> selectCheckItemList(CheckItemQuery checkItemQuery) {
        Page<CheckItemPO> page = new Page<>(checkItemQuery.getCurrent(), checkItemQuery.getPageSize());
        IPage<CheckItemVO> pages = checkItemMapper.selectCheckItemList(page,checkItemQuery);
        return ConvertPageUtil.convertPage(checkItemQuery.getCurrent(),page.getTotal(),pages.getRecords());
    }

    /**
     * 获取检查项详情
     * @param id
     * @return
     */
    @Override
    public CheckItemVO selectCheckItemById(Long id) {
        CheckItemPO checkItem = checkItemMapper.selectOne(new LambdaQueryWrapper<CheckItemPO>()
                .eq(CheckItemPO::getId, id));

        CheckItemVO checkItemVO = new CheckItemVO();
        checkItemVO.setId(checkItem.getId());
        checkItemVO.setDeviceName(checkItem.getDeviceName());
        checkItemVO.setInputeMode(Long.valueOf(checkItem.getInputeMode()));
        checkItemVO.setText(checkItem.getText());
        checkItemVO.setName(checkItem.getName());
        checkItemVO.setIsDeleted(checkItem.getIsDeleted());
        return checkItemVO;
    }

    /**
     * 根据id获取检查项名称
     * @param id
     * @return
     */
    @Override
    public CheckItemVO selectCheckItemName(Long id) {
        CheckItemPO checkItemPO = checkItemMapper.selectOne(new LambdaQueryWrapper<CheckItemPO>()
                .eq(CheckItemPO::getId, id)
                .ne(CheckItemPO::getIsDeleted,1));
        CheckItemVO checkItemVO = new CheckItemVO();
        checkItemVO.setName(checkItemPO.getName());
        checkItemVO.setId(checkItemPO.getId());
        checkItemVO.setDeviceName(checkItemPO.getDeviceName());
        checkItemVO.setText(checkItemPO.getText());
        checkItemVO.setIsDeleted(checkItemPO.getIsDeleted());
        checkItemVO.setInputeMode(Long.valueOf(checkItemPO.getInputeMode()));
        return checkItemVO;
    }
}
